package com.frank.loadinglibrary.common.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;

import java.util.ArrayList;

/**
 * @author SUQI
 * @date 2021/6/1
 * @description
 **/
public class DoubleCircleElement extends BaseElement implements BaseAnimationController.IAnimationController<AnimatorValue> {

    private Paint mPaint;
    private Color[] colors;
    private float[] radius = new float[]{0f, 0f};
    private int duration;
    private float valueZeroPointTwoFive = 0.5f;
    private int valueTwo = 2;

    private DoubleCircleElement(IComponentCallback callback) {
        this.callback = callback;
        initPaint();
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> list = new ArrayList<>();
        AnimatorValue mAnimatorValue = new AnimatorValue();
        mAnimatorValue.setLoopedCount(Animator.INFINITE);
        mAnimatorValue.setDuration(duration);
        mAnimatorValue.setCurveType(Animator.CurveType.LINEAR);
        mAnimatorValue.setValueUpdateListener((animatorValue, v) -> {
            if (v < valueZeroPointTwoFive) {
                radius[0] = 1 - v;
                radius[1] = v;
            } else {
                radius[0] = v;
                radius[1] = 1 - v;
            }
            callback.reflushComponent();
        });
        list.add(mAnimatorValue);
        return list;
    }

    @Override
    public void onAnimationReset() {
        getController().resetAnimation();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        float width = Math.min(mHeight, mWidth);
        float maxRadius = width * 0.5f;
        for (int i = 0; i < valueTwo; i++) {
            mPaint.setColor(colors[i]);
            float mRadiusValue = radius[i] * maxRadius;
            canvas.drawCircle(width * 0.5f, width * 0.5f, mRadiusValue, mPaint);
        }
    }

    @Override
    public BaseAnimationController.IAnimationController<AnimatorValue> initAnimationController() {
        return this;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        mPaint = new Paint();
        mPaint.setStrokeWidth(0);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setAntiAlias(true);
    }

    private void apply(Builder builder) {
        this.colors = builder.colors != null ? builder.colors : new Color[]{Color.GRAY, Color.WHITE};
        this.duration = builder.duration > 0 ? builder.duration : 1000;
    }

    public static class Builder {
        private IComponentCallback iComponentCallback;
        private Color[] colors;
        private int duration;

        public Builder(IComponentCallback callback) {
            this.iComponentCallback = callback;
        }

        public Builder setColors(Color[] colors) {
            this.colors = colors;
            return this;
        }

        public Builder setDuration(int duration) {
            this.duration = duration;
            return this;
        }

        public DoubleCircleElement build() {
            DoubleCircleElement doubleCircleElement = new DoubleCircleElement(iComponentCallback);
            doubleCircleElement.apply(this);
            return doubleCircleElement;
        }
    }
}
